package gold.digger;

import gold.utils.InputUtil;

import java.util.LinkedHashMap;
import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC599 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String[] findRestaurant(String[] list1, String[] list2) {
            Map<String, Integer> posMap = new LinkedHashMap<>();
            for (int i = 0; i < list1.length; i++) {
                posMap.put(list1[i], i);
            }

            List<String> ans = new ArrayList<>();
            int curMinSum = Integer.MAX_VALUE;
            for (int i = 0; i < list2.length; i++) {
                if (posMap.containsKey(list2[i])) {
                    if (posMap.get(list2[i]) + i < curMinSum) {
                        ans.clear();
                        ans.add(list2[i]);
                        curMinSum = posMap.get(list2[i]) + i;
                    } else if (posMap.get(list2[i]) + i == curMinSum) {
                        ans.add(list2[i]);
                    }
                }
            }

            String[] ant = new String[ans.size()];
            for (int i = 0; i < ans.size(); i++) {
                ant[i] = ans.get(i);
            }
            return ant;
        }
    }

    public void run() {
        System.out.println(new Solution().findRestaurant(new String[]{"Shogun", "Tapioca Express", "Burger King", "KFC"}, new String[]{"KFC", "Shogun", "Burger King"}));
    }

    public static void main(String[] args) throws Exception {
        LC599 an = new LC599();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
